﻿using System;
using System.Data;
using System.Collections.Generic;

namespace LDSP.BRL
{
    public partial class Enterprise : BYFW.BOL.BaseConfigEntity
    {
        public Enterprise()
        {
            m_tableName = "t_Enterprise";
            m_hasCode = true;
            m_hasName = true;
            m_hasComment = true;
            m_tblCacheManager.IsEnable = true;
            m_objCacheManager.Size = 1000;
        }

        protected int m_totalDownloadTimes = 0;
        protected int m_downloadTimes = 0;

        /// <summary>
        /// 
        /// </summary>
        public int TotalDownloadTimes
        {
            get { return m_totalDownloadTimes; }
            set { m_totalDownloadTimes = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int DownloadTimes
        {
            get { return m_downloadTimes; }
            set { m_downloadTimes = value; }
        }

        protected override void FillContainer()
        {
            base.FillContainer();
            m_container.Add("totalDownloadTimes", m_totalDownloadTimes);
            m_container.Add("downloadTimes", m_downloadTimes);
        }

        public override bool GetProperties(System.Data.DataRow record)
        {
            if (!base.GetProperties(record))
            {
                return false;
            }
            m_totalDownloadTimes = this.GetIntValue(record, "totalDownloadTimes");
            m_downloadTimes = this.GetIntValue(record, "downloadTimes");
            return true;
        }

        public void ResetDownloadTimes()
        {
            m_downloadTimes = 0;
            this.Update();
        }

        public string GetLogoFileFullName()
        {
            string dir = System.Web.HttpContext.Current.Request.MapPath("~/LogoFiles");
            return System.IO.Path.Combine(dir, m_id + ".png");
        }

        public override int Delete(string transID)
        {
            //删除文件
            string fileFullPath = this.GetLogoFileFullName();
            if (System.IO.File.Exists(fileFullPath))
            {
                System.IO.File.Delete(fileFullPath);
            }
            //删除企业栏目
            List<Catalog> catalogList = new Catalog().GetRootListByEnterpriseID(m_id);
            for (int i = 0; i < catalogList.Count; i++)
            {
                catalogList[i].Delete();
            }
            //防止栏目脏数据
            catalogList = new Catalog().GetSpecialList<Catalog>(null, "enterpriseID", m_id);
            for (int i = 0; i < catalogList.Count; i++)
            {
                catalogList[i].Delete();
            }
            //删除文档
            List<Codification> docList = new Codification().GetSpecialList<Codification>(null, "enterpriseID", m_id);
            for (int i = 0; i < docList.Count; i++)
            {
                docList[i].Delete();
            }
            //删除授权关系
            new R_EnterpriseCatalog().DeleteByEnterpriseID(m_id);

            //删除适用关系
            List<R_EnterpriseCodification> ecList = new R_EnterpriseCodification().GetListByEnterprise(m_id);
            for (int i = 0; i < ecList.Count; i++)
            {
                ecList[i].Delete();
            }

            //删除企业用户
            List<EnterpriseUser> userList = new EnterpriseUser().GetSpecialList<EnterpriseUser>(null, "enterpriseID", m_id);
            for (int i = 0; i < userList.Count; i++)
            {
                userList[i].Delete();
            }
            return base.Delete(transID);
        }
    }
}
